<?php
require_once(dirname(__FILE__) . '/app.php');

Logs::setLog("1");
$defaultShopId = $_REQUEST['shopId'];
if($defaultShopId < 1){
	$defaultShopId = 1;
}
$shopList = Shop::GetAllShops();
Logs::setLog("2");
$serviceList = Service::GetAllByShopId($defaultShopId);
$deviceList = Devices::GetAllByShopId($defaultShopId);
$staffList = Staff::GetAllByShop($defaultShopId);
Logs::setLog("3");
$serviceId = $_REQUEST['service_id'];

$deviceId = $_REQUEST['device_id'];
$staffId = $_REQUEST['staff_id'];
$deviceId = $_REQUEST['device_id'];

$searchDate = $_REQUEST['search_date'];
$getDateArray = array();

if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}
$no_selectStaffId="";
$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$getTimeArray = explode(" ", $getDateArray[2]);
$day = $getTimeArray[0] * 1;

$startHour = $_REQUEST['startHour'] * 1;
$startMinute = $_REQUEST['startMinute'] * 1;

$tmp_date=date("YmdHi");
$year = $year > 0 ? $year : substr($tmp_date,0,4) * 1;
$month = $month > 0 ? $month : substr($tmp_date,4,2) * 1;
$day = $day > 0 ? $day : substr($tmp_date, 6, 2) * 1;
$startHour = $startHour > 0 ? $startHour : substr($tmp_date,8,2) * 1;
//$startMinute = $startMinute > 0 ? $startMinute : substr($tmp_date, 10, 2) * 1;


if( is_post() ){
	Logs::setLog("4");
	
	$endHour = $_POST['endHour'];
	$endMinute = $_POST['endMinute'];
	
	$service = Service::GetById($serviceId);
	$shop = Shop::getById($defaultShopId);
	if($endHour < 1){
		$endMinute = $startMinute + $service['service_duration'];
		$endHour = $startHour;
		while ($endMinute > 59){
			$endMinute = $endMinute - 60;
			$endHour = $endHour + 1;
		}
	}
	$result = true;
	if( $startHour < $shop['open_hour'] || $endHour > $shop['close_hour'] ){
		Logs::setLog("sdfsdfdsfdsfdsfdsfdsf||$startHour");
		//Logs::setLog('sfdsdf.1111||'.$shop['open_hour'] + "||".$shop['close_hour']."||".$startHour."||".$endHour);
		$result = false;
	}else if( ($startHour == $shop['open_hour'] && $startMinute < $shop['open_minute']) || 
		($endHour == $shop['close_hour'] && $endMinute > $shop['close_minute']) ){
		Logs::setLog('予約時間不正');
		Session::Set('error', '予約時間不正');
		$result = false;
	}
	Logs::setLog("5");
	if( $result ){
		$result = ShopOrderLimit::CanBook($defaultShopId, $service, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		
		
	}
	if( $result == 1 ){
		Logs::setLog("6");
		$startTime = mktime($startHour, $startMinute, 0, $month, $day, $year);
		$endTime = mktime($endHour, $endMinute, 0, $month, $day, $year);
		if( $staffId < 1 ){
			//スタッフ指名がない場合はback1ニ何かしらのデータを入れておく
			Logs::setLog("7");
			$no_selectStaffId = "YES";
			for($index = 1; $index < 5; $index++){
				$staffIds = StaffSkill::GetStaffsBySkillAndLevle($serviceId, $index);
				foreach( $staffIds as $k => $v ){
					$tempStaff = Staff::getById( $v['staff_id'] );
					if( $tempStaff['activity'] == Staff::$FLAG_UNACTIVE ){
						continue;
					}
					
					$temp = WorkSchedule::HasWork($startTime, $endTime, $v['staff_id']);
					if( $temp == null ){
						continue;
					}
					$temp = Preengage::HasOrdered($startTime, $endTime + $service['reset_device_time'], $v['staff_id']);
					if($temp != null){
						continue;
					}
					
					$temp = Absence::HasAbsence($startTime, $endTime, $v['staff_id']);
					if($temp != null){
						continue;
					}
					
					$staffId = $v['staff_id'];
					break;
				}
				if( $staffId > 0 ){
					Logs::setLog("8");
					break;
				}
			}

		}else{
			//その時間にその店員は働いているか
			$temp = Preengage::HasOrdered($startTime, $endTime + $service['reset_device_time'], $staffId);
			//できるやつがいるか
			$temp1 = StaffSkill::GetByStaffAndSkill($staffId, $serviceId);
			if($temp != null || $temp1 == null){
				
				Logs::setLog("9");
				Session::Set('error', 'ご指名スタッフはこの時間で予約取れませんの為に、ご予約できませんでした。');
				include template('preengage_succ');
				exit;
			}
			
			
		}
		if($deviceId < 1){
			
			for($index = 1; $index < 5; $index++){
				if( $deviceId > 0 ){
					break;
				}
				
				$deviceIds = DeviceUse::GetDevicesBySkillAndLevel($serviceId, $index);
				foreach( $deviceIds as $k => $v ){
					$temp = Devices::GetById($v['device_id']);
					$usedCount = Preengage::GetUsedDeviceCount($defaultShopId, $serviceId, $v['device_id'], $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
					$usedCount = $usedCount[0];
					if( $temp['device_count'] <= $usedCount['count'] ){
						continue;
					}
					$deviceId = $v['device_id'];
					break;
				}
				
			}
		}
		
		if( $staffId < 1 ){
			Logs::setLog(" zui shi hua  kaishi ");
			$otherOrderStaff = ShopOrderLimit::GetOtherOrderStaff($serviceId, $startTime, $endTime);
			
			foreach ($otherOrderStaff as $k => $v){
				Logs::setLog(" zui shi hua  kaishi 1 ".$v['staff_id']);
				if( $staffId > 0 ){
					break;
				}
				
				$temp = StaffSkill::GetByStaffAndSkill($v['staff_id'], $serviceId);
				if( $temp == null ){
					continue;
				}
				
				$temp = Absence::HasAbsence($startTime, $endTime, $v['staff_id']);
				if( $temp != null ){
					continue;
				}
				
				$temp = Preengage::HasOrderedSuchService($startTime, $endTime, $v['staff_id'], $serviceId);
				Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 2 ".$v['staff_id']);
				if( $temp != null ){
					continue;
				}
				Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 3 ".$v['staff_id']);
				$temp = Preengage::HasOrdered($startTime, $endTime, $v['staff_id']);
				Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 4 ".$v['staff_id']);
				$replaceAble = true;
				$replaceAbleStaffId = array();
				foreach($temp as $k1 => $v1){
					Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 5 ".$v1['id']);
					$tempStaffs = StaffSkill::GetStaffsBySkill( $v1['service_id'] );
					foreach($tempStaffs as $k2 => $v2){
						if($v2['staff_id'] == $v1['designation_stuff']){
							continue;
						}
						
						$result = Utility::able2work($v2['staff_id'], $v1['service_start_time'], $v1['real_end_time']);
						Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 6 ".$v2['staff_id']);
						if( !$result || $v2['staff_id'] == $v1['designation_stuff']){
							continue;
						}
						Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 7 ".$v2['staff_id']);
						$replaceAbleStaffId[$k1] = $v2['staff_id'];
						break;
					}
					Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 8 ".$v2['staff_id']);
					if($replaceAbleStaffId[$k1] < 1){
						Logs::setLog(" zui shi hua  kaishi 1<$serviceId> 9 ".$v2['staff_id']);
						$replaceAble = false;
						break;
					}
				}
				
				if( $replaceAble ){
					foreach($temp as $k1 => $v1){
						$v1['designation_stuff'] = 	$replaceAbleStaffId[$k1];	
						Preengage::UpdateByOrige($v1);
					}
					$staffId = $v['staff_id'];
				}
				
			}
			
			//その時間にその店員は働いているか
			$temp = Preengage::HasOrdered($startTime, $endTime + $service['reset_device_time'], $staffId);
			//できるやつがいるか
			$temp1 = StaffSkill::GetByStaffAndSkill($staffId, $serviceId);
			if($temp != null || $temp1 == null){
				Session::Set('error', '大変申し訳ございません。お客様ご予約したい時間帯は込み合っている為に予約取れませんでした。');
				include template('preengage_succ');
				exit;
			}
			
		
		}
		
		if( $staffId < 1 || $deviceId < 1 ){
			if($no_selectStaffId == "YES"){
				//var_dump($no_selectStaffId.$staffId.$deviceId);
				//再度検索
				Preengage::CheckOtherStaff($startTime - $service['reset_device_time'], $endTime,$deviceId,$login_user_id, $serviceId, $defaultShopId, $startTime, $endTime, $service['service_price'], $staffId, $_REQUEST['staff_id'] > 0);
			}
			else{
				Logs::setLog(" order error $staffId, $deviceId");
				Session::Set('error', '大変申し訳ございません。お客様ご予約したい時間帯は込み合っている為に予約取れませんでした。');
			}
		}else{
			Logs::setLog(" order succ $staffId, $deviceId, $defaultShopId");
			if($no_selectStaffId == "YES"){
				Preengage::Create($login_user_id, $serviceId, $defaultShopId, $startTime, $endTime, $service['service_price'], $staffId, $deviceId,0,0,0,0,"YES");}
			else{Preengage::Create($login_user_id, $serviceId, $defaultShopId, $startTime, $endTime, $service['service_price'], $staffId, $deviceId, $_REQUEST['staff_id'] > 0);}
			$staff = Staff::getById($staffId);
			Utility::sendMail($staff, null, null);
			Session::Set('msg', 'ご予約有難う御座いました。ご来店待ちしております。');
		}
	}else{
		Logs::setLog("予約上限になりましたので、予約できません。<$year, $month, $day, $startHour, $startMinute, $endHour, $endMinute>");
		Session::Set('error', '大変申し訳ございません。お客様ご予約したい時間帯は込み合っている為に予約取れませんでした。');
	}
	include template('preengage_succ');
	exit;
}

Logs::setLog("7");



include template('preengage');
//include template('subscribe');
